//
//  PlistChooseController.m
//  HuLaQuan
//
//  Created by liyan on 16/1/22.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "PlistChooseController.h"
//tableView cell
#import "PlistSearch.h"
#import "PlistLabel.h"
#import "PlistCity.h"
#import "ShowCityCell.h"

@interface PlistChooseController ()<UISearchBarDelegate,UISearchDisplayDelegate>

//cityShow
@property (nonatomic, strong) NSMutableDictionary *cities;
@property (nonatomic, strong) NSMutableArray *keys; //城市首字母
@property (nonatomic, strong) NSMutableArray *arrayCitys;   //城市数据

//selectedCityName
@property(nonatomic,strong)NSString *cityName;

@end

@implementation PlistChooseController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)setupUI
{
    self.title = @"选择城市";
    [self setupNavBackButton];
    self.keys = [NSMutableArray array];
    self.arrayCitys = [NSMutableArray array];
     [self getCityData];
    //regist
    [self.tableView registerNib:[UINib nibWithNibName:@"PlistSearch" bundle:nil] forCellReuseIdentifier:@"search"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PlistLabel" bundle:nil] forCellReuseIdentifier:@"label"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PlistCity" bundle:nil] forCellReuseIdentifier:@"city"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShowCityCell" bundle:nil] forCellReuseIdentifier:@"show"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //改变索引的颜色
    self.tableView.sectionIndexColor = [UIColor grayColor];
    //改变索引选中的背景颜色
    //self.tableView.sectionIndexTrackingBackgroundColor = [UIColor grayColor];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    
}
#pragma mark - 获取城市数据
-(void)getCityData
{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"citydict"
                                                   ofType:@"plist"];
    
   // NSLog(@"path:%@",pathCity);
    self.cities = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"city:%@",self.cities);
    
    [self.keys addObjectsFromArray:[[self.cities allKeys] sortedArrayUsingSelector:@selector(compare:)]];
     //NSLog(@"keys:%@",self.keys);
    [self.tableView reloadData];
}

#pragma mark - tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_keys count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return 1;
            break;
        case 3:
            return 1;
            break;
        default:
        {
            NSString *key = [_keys objectAtIndex:section];
            NSArray *citySection = [_cities objectForKey:key];
            return [citySection count];
        }
            break;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        PlistSearch *cell = [tableView dequeueReusableCellWithIdentifier:@"search"];
        return cell;
    }
    else if (1 == indexPath.section || 3 == indexPath.section)
    {
        PlistLabel *cell = [tableView dequeueReusableCellWithIdentifier:@"label"];
        if (1 == indexPath.section) {
            cell.plistLabel.text = @"当前定位城市";
        }
        else
        {
            cell.plistLabel.text = @"选择城市";
        }
        return cell;
    }
    else if (2 == indexPath.section)
    {
        PlistCity *cell = [tableView dequeueReusableCellWithIdentifier:@"city"];
        cell.tag = 150;
        if (self.cityName) {
            cell.cityName.text = self.cityName;
        }
        else
        {
            cell.cityName.text = @"贵阳";
        }
        return cell;
    }
    else
    {
        NSString *key = [_keys objectAtIndex:indexPath.section];

        ShowCityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"show"];
        cell.tag = indexPath.row;
        //ShowCityCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell.textLabel setTextColor:[UIColor blackColor]];
        cell.textLabel.font = [UIFont systemFontOfSize:17];
        cell.textLabel.text = [[_cities objectForKey:key] objectAtIndex:indexPath.row];
        
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section <= 3) {
        return 0;
    }
    else
    {
        return 20;
    }
        
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if ( section > 3) {
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
        bgView.backgroundColor = [UIColor lightGrayColor];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, 250, 20)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.font = [UIFont systemFontOfSize:12];
        
        NSString *key = [_keys objectAtIndex:section];
        
        titleLabel.text = key;
        
        [bgView addSubview:titleLabel];
        
        return bgView;
    }
    else
        return nil;
}
//返回索引的titles
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
  
    return _keys;
}

//select
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section <= 3) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    if (indexPath.section > 3) {
        UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        NSLog(@"%@",cell.textLabel.text);
        self.cityName = cell.textLabel.text;
        
        PlistCity *cityCell = (PlistCity *)[self.view viewWithTag:150];
        cityCell.cityName.text = nil;
        cityCell.cityName.text = self.cityName;
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
